package pl.iachoo.ctl.tabory.ui.base.masterdetail;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import pl.iachoo.ctl.tabory.ui.properties.Messages;
import pl.iachoo.ctl.tabory.ui.selection.ISelection;
import pl.iachoo.ctl.tabory.ui.selection.ISelectionProvider;

public abstract class AbstractMasterSectionPart<T extends ISelection> extends
		SectionPart implements ISelectionProvider<T> {

	protected T selection;
	protected String sectionName;
	protected Section parentSection;
	protected FormToolkit toolkit;

	protected Table table;
	protected TableViewer tableViewer;

	protected ToolBar toolBar;
	protected ToolItem toolItemNew;
	protected ToolItem toolItemEdit;
	protected ToolItem toolItemDelete;

	public AbstractMasterSectionPart(Composite parent, FormToolkit toolkit,
			int style, String sectionName) {
		super(parent, toolkit, style);
		this.toolkit = toolkit;
		this.sectionName = sectionName;
		parentSection = getSection();
		parentSection.setText(sectionName);
	}

	protected abstract void initGui(Section parentSection, FormToolkit toolkit);

	protected abstract void postInitGui(Section parentSection,
			FormToolkit toolkit);

	protected abstract void initActions();

	protected abstract void refreshToolBar();

	protected void setEnabled(boolean isEnabled) {
		if (table != null) {
			table.setEnabled(isEnabled);
		}
		if (tableViewer != null) {
			toolBar.setEnabled(isEnabled);
		}
	}

	protected ToolBar getToolBar(Section parentSection, FormToolkit toolkit) {
		ToolBar toolBar = new ToolBar(parentSection, SWT.FLAT | SWT.HORIZONTAL);
		toolItemNew = new ToolItem(toolBar, SWT.FLAT);
		toolItemNew.setImage(PlatformUI.getWorkbench().getSharedImages()
				.getImage(ISharedImages.IMG_TOOL_NEW_WIZARD));
		toolItemNew.setToolTipText(Messages.getString("common.addnew"));
		toolItemEdit = new ToolItem(toolBar, SWT.PUSH);
		toolItemEdit.setImage(PlatformUI.getWorkbench().getSharedImages()
				.getImage(ISharedImages.IMG_TOOL_COPY));
		toolItemEdit.setToolTipText(Messages.getString("common.edit"));
		toolItemDelete = new ToolItem(toolBar, SWT.PUSH);
		toolItemDelete.setImage(PlatformUI.getWorkbench().getSharedImages()
				.getImage(ISharedImages.IMG_TOOL_DELETE));
		toolItemDelete.setToolTipText(Messages.getString("common.delete"));
		toolkit.paintBordersFor(toolBar);
		return toolBar;
	}

}
